package com.briup.day15;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Test_HashSet {
    public static void main(String[] args) {
        //给我们set集合插入自定义类 重写equals方法
        Set<Student> set = new HashSet<Student>();
        Student s1 = new Student("tom",20);
        Student s2 = new Student("tom",20);
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        set.add(s1);
        set.add(s2);
        System.out.println(set);
        set.add(new Student("tom",20));
        set.add(new Student("tom",20));
        for(Student s : set){
            System.out.println(s);
        }
        System.out.println(set.size());

    }
}
class Student{
    private String name;
    private int age;
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    public Student() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Student)) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
